home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Converters / aa_m68k_Intel_HP_Only / ToMPEG.0.8 / Source / tiff2ppm.m < prev    next >
Encoding:
Text File  |  1994-11-14  |  4.5 KB  |  157 lines

  1. // tiff2ppm.m: Translate a TIFF file to PPM format
  2. // Usage: tiff2ppm tifffile  > ppmfile
  3. // Compile with: 
  4. //   cc -O -g tiff2ppm.m -o tiff2ppm -lsys_s -lNeXT_s -arch m68k -arch i386
  5. // Written by cahalan@clouds.gsfc.nasa.gov, October, 1994.
  6. #include <stdio.h>
  7. #import <dpsclient/event.h>
  8. #import <appkit/appkit.h>
  9. #define maxVal 255 
  10. #define HI    240  // upper 4 bits
  11. #define LO    15   // lower 4 bits
  12.  
  13.     struct Color {unsigned char *R; unsigned char *G; unsigned char *B;
  14.                 unsigned char *A;};
  15.     struct Color *pColor;
  16.     BOOL isPlanar = FALSE, hasAlpha = FALSE;
  17.     int width, height, samples = 3, bitsPerSample = 8, numColors = 3;
  18.     unsigned char r, g, b, invert = maxVal;
  19.     int colorSpace = NX_RGBColorSpace;
  20.  
  21. void die(char *s) {fprintf(stderr,"%s\n",s);exit(1);}
  22.  
  23. void writePPM(void)
  24.     {
  25.     register int x, y, i;
  26.  
  27.     fprintf(stdout, "P6\n");
  28.     fprintf(stdout, "%d %d\n", width, height);
  29.     fprintf(stdout, "%d\n", maxVal);
  30.  
  31. //   ********************* grayscale with "min-is-white"
  32.     if (samples==1 && bitsPerSample == 8) {
  33.         if(colorSpace == NX_OneIsWhiteColorSpace) invert = 0;
  34.         for (y=0; y<height; y++) for (x=0; x<width; x++)  {
  35.             r = *pColor->R++ ^ invert; 
  36.             for (i=0; i<3; i++) putchar(r);
  37.         }
  38.     return;
  39.     }
  40.  
  41. //   ********************* increment Color ptr for planar or non-planar
  42.         switch(bitsPerSample) {
  43.             case 1: 
  44.             case 2: die("Can't do monochrome!\n");
  45.                 break;
  46.             case 4: 
  47.                  if(isPlanar) {samples = samples >> 1;
  48.                   for (y=0; y<height; y++) for (x=0; x<width/2; x++)  {
  49.                 r = *pColor->R; g = *pColor->G; b = *pColor->B;
  50.                 putchar(r & HI);
  51.                 putchar(g & HI);
  52.                 putchar(b & HI);
  53.                 putchar(r << 4);
  54.                 putchar(g << 4);
  55.                 putchar(b << 4);
  56.                 pColor->R++; pColor->G++; pColor->B++; 
  57.                  } }
  58.                  else if(hasAlpha) { samples = samples >> 1;
  59.                   for (y=0; y<height; y++) for (x=0; x<width; x++)  {
  60.                 r = *pColor->R; g = *pColor->G;
  61.                 putchar(r & HI);
  62.                 putchar(r << 4);
  63.                 putchar(g & HI);
  64.                 pColor->R += samples; 
  65.                 pColor->G += samples; 
  66.                  } }
  67.                  else for (y=0; y<height; y++) for (x=0; x<width/2; x++)  {
  68.                 r = *pColor->R; g = *pColor->G; b = *pColor->B;
  69.                 putchar(r & HI );
  70.                 putchar(r << 4);
  71.                 putchar(g & HI);
  72.                 putchar(g << 4);
  73.                 putchar(b & HI);
  74.                 putchar(b << 4);
  75.                 pColor->R += samples; 
  76.                 pColor->G += samples; 
  77.                 pColor->B += samples;
  78.                  }
  79.                 break;
  80.             case 8: 
  81.                 for (y=0; y<height; y++) for (x=0; x<width; x++)  {
  82.                 fwrite(pColor->R, 1, 1, stdout);
  83.                 fwrite(pColor->G, 1, 1, stdout);
  84.                 fwrite(pColor->B, 1, 1, stdout);
  85.                 if(isPlanar) {pColor->R++; pColor->G++; pColor->B++;}
  86.                 else {
  87.                     pColor->R += samples; 
  88.                     pColor->G += samples; 
  89.                     pColor->B += samples;
  90.                 }
  91.                  }
  92.                 break;
  93.             default: die("Don't recognize this TIFF type!\n");
  94.                 break;
  95.         }
  96.     }
  97.  
  98.  
  99. void main(int argc, char *argv[])
  100. {
  101.    id tiff;
  102.    unsigned char *planeData[5] = {NULL, NULL, NULL, NULL, NULL};
  103.    unsigned char *meshedData = NULL;
  104.    NXSize size;
  105.  
  106. /*   ********************* check usage  ****************
  107.    if(argc > 1) {
  108.     fprintf(stderr, "argc = %d\n", argc);
  109.     fprintf(stderr, "Translating %s to PPM\n", argv[1]);
  110.    }
  111.    else die("Usage: tiff2ppm tiffFile > ppmFile");
  112. */
  113.  
  114. //   ********************* read TIFF, load into imageRep *****
  115.    tiff = [[NXBitmapImageRep alloc] initFromFile:argv[1]];
  116.  
  117. //   ********************* get a pointer to the image data *****
  118.    [tiff getDataPlanes:planeData];
  119. /*   fprintf(stderr, "planeData[] = {%ld, %ld, %ld, %ld, %ld}\n",
  120.             planeData[0], planeData[1], planeData[2],
  121.             planeData[3], planeData[4]);
  122. */
  123. //  ********************* get image dimensions, etc
  124.    [tiff getSize:&size];
  125.    width = size.width;
  126.    height = size.height;
  127.    samples = [tiff samplesPerPixel];
  128.    bitsPerSample = [tiff bitsPerSample];
  129.    numColors = [tiff numColors];
  130.    hasAlpha = [tiff hasAlpha];
  131.    colorSpace = [tiff colorSpace];
  132. /*
  133. fprintf(stderr, "width = %d\t height = %d\n", width, height);
  134. fprintf(stderr, "samples = %d\t bitsPerSample = %d\n", samples, bitsPerSample);
  135. fprintf(stderr, "numColors = %d\t hasAlpha = %d\t colorSpace = %d\n", 
  136.                 numColors, hasAlpha, colorSpace);
  137. */
  138. //   ********************* init Color ptr for planar or non-planar
  139.    pColor = malloc(sizeof(struct Color));
  140.    if(isPlanar = [tiff isPlanar]) {
  141.        pColor->R = planeData[0];
  142.     pColor->G = planeData[1];
  143.     pColor->B = planeData[2];
  144.    } else {
  145.        meshedData = planeData[0];
  146.     pColor->R = meshedData;
  147.     pColor->G = meshedData+1; 
  148.     pColor->B = meshedData+2;
  149.    }
  150.  
  151. //   ********************* write out the PPM file
  152.    writePPM();
  153.   
  154. //  ********************* cleanup  ****************************
  155.    [tiff free];
  156.  }
  157.